﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using Agos.Utils;

namespace DeutscheRemoval
{
    public partial class Configurazione : Form
    {
        public Configurazione()
        {
            InitializeComponent();
        }

        private void Configurazione_Load(object sender, EventArgs e)
        {
            FillComponents();
        }

        private void FillComponents()
        {
            textBoxLog.Text = ConfigUtil.GetLog();
            comboSSL.SelectedIndex = 0;
            comboBoxOra.Text = ConfigUtil.GetHour();
            comboBoxMinuto.Text = ConfigUtil.GetMinute();
            textBoxExcel.Text = ConfigUtil.GetExcelPath();
            textBoxExcelName.Text = ConfigUtil.GetExcelName();
            textBoxFirstPath.Text = ConfigUtil.GetFirstSearchPath();
            textBoxSecondPath.Text = ConfigUtil.GetSecondSearchPath();
            textBoxOutputPath.Text = ConfigUtil.GetOutputFilePath();
            textBoxOutputExcel.Text = ConfigUtil.GetOutputFileName();
            txtSMTP.Text = ConfigUtil.GetSMTPServer();
            txtPORT.Text = ConfigUtil.GetSMTPPort();
            txtEmailSender.Text = ConfigUtil.GetSender();
            txtPasswordSender.Text = ConfigUtil.GetSenderPassword();
            comboSSL.Text = ConfigUtil.GetSSL();
            txtEmailStatus.Text = ConfigUtil.GetStatusEmail();
        }

        private void buttonSalva_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void Save()
        {
            try
            {
                if (
                     textBoxLog.Text != string.Empty
                    && comboBoxOra.Text != string.Empty
                    && comboBoxMinuto.Text != string.Empty
                    && textBoxExcel.Text != string.Empty
                    && textBoxExcelName.Text != string.Empty
                    && textBoxOutputExcel.Text != string.Empty
                    && textBoxOutputPath.Text != string.Empty
                    && txtEmailSender.Text != string.Empty
                    && txtEmailStatus.Text != string.Empty
                   )
                {
                    Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    cfg.AppSettings.Settings["Hour"].Value = comboBoxOra.Text;
                    cfg.AppSettings.Settings["Minute"].Value = comboBoxMinuto.Text;

                    cfg.AppSettings.Settings["InputFilePath"].Value = textBoxExcel.Text;
                    cfg.AppSettings.Settings["FirstSearchPath"].Value = textBoxFirstPath.Text;
                    cfg.AppSettings.Settings["OutputFilePath"].Value = textBoxOutputPath.Text;
                    cfg.AppSettings.Settings["OutputFileName"].Value = textBoxOutputExcel.Text;
                    cfg.AppSettings.Settings["SecondSearchpath"].Value = textBoxSecondPath.Text;
                    cfg.AppSettings.Settings["StatusEmail"].Value = txtEmailStatus.Text;
                    cfg.AppSettings.Settings["Port"].Value = txtPORT.Text;
                    cfg.AppSettings.Settings["Port"].Value = txtPORT.Text;
                    cfg.AppSettings.Settings["Sender"].Value = txtEmailSender.Text;
                    
                    cfg.Save(ConfigurationSaveMode.Full);
                    ConfigurationManager.RefreshSection("appSettings");
                    MessageBox.Show("Configurazioni salvate!");
                }
                else
                {
                    MessageBox.Show("Make sure all fields are completed.");
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString(), LogType.Error);
            }
        }

        private void buttonAnulla_Click(object sender, EventArgs e)
        {
            FillComponents();
        }

        private void buttonLog_Click(object sender, EventArgs e)
        {
            OpenFolderBrowser(textBoxLog);
        }

        private void OpenFolderBrowser(TextBox textBox)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            if (fbd.ShowDialog() == DialogResult.OK)
            {
                textBox.Text = fbd.SelectedPath + "\\";
            }
        }

        private void buttonExcel_Click(object sender, EventArgs e)
        {
            OpenFolderBrowser(textBoxExcel);
        }


        //mail config button
        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFolderBrowser(textBoxLog);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFolderBrowser(textBoxFirstPath);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFolderBrowser(textBoxOutputPath);

        }
    }
}
